const Webpack = require("webpack");
const path = require("path");
// const TerserPlugin = require("terser-webpack-plugin");
const WebpackObfuscator = require("webpack-obfuscator");

const addZero = (num) => {
    return ("00" + num).slice(-2);
};
const now = new Date();
now.setMinutes(now.getMinutes() + now.getTimezoneOffset() + 60 * 8);
const __VERSION__ = `${now.getFullYear()}${addZero(now.getMonth() + 1)}${addZero(now.getDate())}${addZero(now.getHours())}${addZero(now.getMinutes())}`;

const __DEV__ = process.env.NODE_ENV === "development";

const plugins = [
    new Webpack.DefinePlugin({
        __DEV__: __DEV__,
        __VERSION__: __VERSION__,
    }),
];

// if (!__DEV__) {
//     plugins.push(
//         new WebpackObfuscator({
//             compact: true,
//             unicodeEscapeSequence: true,
//             selfDefending: JSON.parse(process.env.NODE_COMPACT_SELF_DEFENDING),
//         })
//     );
// }

module.exports = {
    publicPath: "./",
    lintOnSave: false,
    productionSourceMap: false,
    pages: {
        index: {
            entry: 'src/main.ts',
            template: 'public/index.html',
            filename: 'index.html',
        },
        index3: {
            entry: 'src/main.ts',
            template: 'public/index3.html',
            filename: 'index3.html',
        },
    },
    css: {
        loaderOptions: {
            sass: {
                prependData: `
                    @import "app/assets/css/variables.modules.scss";
                    @import "@/assets/css/variables.modules.scss";
                `,
            },
        },
    },
    configureWebpack: {
        resolve: {
            alias: {
                app: path.resolve(__dirname, "kaiosapp"),
                public: path.resolve(__dirname, "public"),
            },
        },
        plugins: plugins,
    },
    chainWebpack: (config) => {
        config.module
            .rule("webapp")
            .test(/\.webapp$/)
            .use("json-loader")
            .loader("json-loader")
            .end();
    },
};
